package com.easonji.admin.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import com.easonji.admin.dao.UserDAO;
import com.easonji.admin.manager.UserManager;
import com.easonji.admin.mapper.UserAppMapper;
import com.easonji.admin.mapper.UserMapper;
import com.easonji.admin.model.User;
import com.easonji.admin.model.UserApp;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.easonji.admin.manager.AppManager;
import com.easonji.admin.manager.RoleManager;
import com.easonji.admin.mapper.UserRoleMapper;
import com.easonji.admin.model.UserRole;
import com.easonji.common.constant.Consts;
import com.easonji.common.constant.cache.CacheConsts;
import com.easonji.common.constant.cache.CacheKeyConsts;
import com.easonji.common.util.StringUtils;

/**
 * @ClassName: UserManagerImpl
 * @Description: 用户通用类实现
 * @Author: LiuJun
 * @date: 2018/07/29 07:48
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Service
public class UserManagerImpl implements UserManager {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserDAO userDAO;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleManager roleManager;

    @Resource
    private UserAppMapper userAppMapper;

    @Resource
    private AppManager appManager;

    @Override
    public Integer getUserIdByAccount(String account) {
        Integer result = null;
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("account", account);
        List<User> users = userMapper.selectList(wrapper);
        if (StringUtils.isNotEmpty(users)) {
            result = users.get(0).getDeptId();
        }
        return result;
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_NAME + "'+#userId")
    public String getUserNameById(Integer userId) {
        String result = "";
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("id", userId);
        List<User> users = userMapper.selectList(wrapper);
        if (StringUtils.isNotEmpty(users)) {
            result = users.get(0).getName();
        }
        return result;
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_ACCOUNT + "'+#userId")
    public String getUserAccountById(Integer userId) {
        String result = "";
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("id", userId);
        List<User> users = userMapper.selectList(wrapper);
        if (StringUtils.isNotEmpty(users)) {
            result = users.get(0).getAccount();
        }
        return result;
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_DEPT_ID + "'+#userId")
    public Integer getUserDeptIdById(Integer userId) {
        Integer result = null;
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("id", userId);
        List<User> users = userMapper.selectList(wrapper);
        if (StringUtils.isNotEmpty(users)) {
            result = users.get(0).getDeptId();
        }
        return result;
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_ROLE_NAME + "'+#userId")
    public String getRoleNameByUserId(Integer userId) {
        Wrapper<UserRole> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserRole> userRoles = userRoleMapper.selectList(wrapper);
        if (StringUtils.isEmpty(userRoles)) {
            return "--";
        }
        StringBuilder roleIds = new StringBuilder();
        userRoles.forEach(userRole -> {
            roleIds.append(Consts.COMMA);
            roleIds.append(userRole.getRoleId());
        });
        return roleManager.getRoleNamesByIds(roleIds.substring(1));
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_APP_NAME + "'+#userId")
    public String getAppNameByUserId(Integer userId) {
        Wrapper<UserApp> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserApp> userApps = userAppMapper.selectList(wrapper);
        if (StringUtils.isEmpty(userApps)) {
            return "--";
        }
        StringBuilder appIds = new StringBuilder();
        userApps.forEach(userApp -> {
            appIds.append(Consts.COMMA);
            appIds.append(userApp.getAppId());
        });
        return appManager.getAppNamesByIds(appIds.substring(1));
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_ROLE_ID_LIST + "'+#userId")
    public List<Integer> getRoleIdListByUserId(Integer userId) {
        return userDAO.selectRoleIdByUserId(Consts.DEFAULT_APP_ID, userId);
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.USER_ROLE_NAME_LIST + "'+#userId")
    public List<String> getRoleNameListByUserId(Integer userId) {
        return userDAO.selectRoleNameByUserId(Consts.DEFAULT_APP_ID, userId);
    }
}
